/*用户的增删改查*/
const router = require('koa-router')(),
    DB = require('../../module/db.js'),
    md5 = require('../../module/tools.js'),
    upload = require('../../utils/upload.js');
     

/**
 * @router GET /admin/user/list
 * @desc   用户列表接口地址  返回
 */
router.get('/list',async (ctx)=>{
    let data = await DB.find('user')   
    
    if(!data[0]){
        ctx.body = {
            code: 0,
            status: false,
            msg:"无数据"
        }
        return false
    }
    ctx.body = {
        code: 200,
        status: true,
        data: data,
    }

})

router.get('/',async (ctx)=>{
    let username = ctx.request.body.username;
    let data = await DB.find('user',{"username":username})
    ctx.body = data;    
    // await ctx.render('admin/user/member-list',{
    //     list:data
    // });

})

/**
 * @router POST /admin/user/userAdd
 * @desc   添加接口地址  返回
 */
router.post('/userAdd', async (ctx, next) => {
    let { username, password = '123456', phone, sex="", desc="",email=""} = ctx.request.body;
    //先查询数据库用户名是否存在
    let  data = await DB.find('user',{"username": username})
    //md5加密
    var md5Pass = await md5.MD5(password);
    if(data[0]){
        ctx.body = {
            code: 0,
            status: false,
            msg:"账号已注册"
        }
        return false
    }
    try{
        let userAdd = await DB.insert('user',
        {
            username: username,
            password: md5Pass,
            sex: sex,
            desc: desc,
            phone: phone,
            email: email,
            reg_time: Date.now()
        });
        if(userAdd.result.ok){
            ctx.body = {
                code: 200,
                status: false,
                msg:"账号注册成功"
            }
        }
        return
    }catch(err){
        ctx.body = {
            code: 400,
            status: false,
            msg: err
        }
    }
    
});

/**
 * @router POST /admin/user/userEdit
 * @desc   编辑用户接口地址  返回
 */
router.post('/userEdit',async(ctx,next)=>{
    // const user = ctx.request.body;
    let { _id,username, password, phone, sex="", desc="",email=""} = ctx.request.body;
    //先查询数据库用户名是否存在
    let  data = await DB.find('user',{"username": username})
    if(data[0] && data[0]._id != _id){
        console.log(data[0]._id , _id)
        ctx.body = {
            code: 0,
            status: false,
            msg:"用户名已存在"
        }
        return false
    }
    try{
        let userEdit = await DB.update('user',{"_id":DB.getObjectId(_id)},{
            "username": username || data[0].username,
            "phone": phone || data[0].phone,
            "sex": sex || data[0].sex,
            "email": email || data[0].email,
            "desc": desc || data[0].desc,
        });
        if(userEdit.result.ok){
            ctx.body = {
                code: 200,
                status: true,
                msg:"修改成功"
            }
        }
        return
    }catch(err){
        ctx.body = {
            code: 400,
            status: false,
            msg:err
        }
    } 
})
 
/**
 * @router POST /admin/user/userDelete
 * @desc   删除用户接口地址  返回
 */
router.post('/userDelete', async (ctx, next) => {
    let id = ctx.request.body.id;
    if (id) {
        let data = await DB.remove('user',{"_id":DB.getObjectId(id)});
        try{
            if (data.result.ok) {
                ctx.body = {
                    code: 200,
                    status: true,
                    msg:"删除成功"
                }
            }
        }catch (err) {
            ctx.body = {
                code: 400,
                status: false,
                msg: err
            }
            return;
        }
    }
  });

// router.get('upload',async (ctx) => {
// await ctx.render('upload'));

router.post('/upload',upload.single('file'),async (ctx) => {
    console.log(ctx.req.file);
    ctx.body = ctx.request.body;
});

module.exports = router.routes();